﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;

namespace Sunny.Controls
{
	public class IgnoreControlList<T> : System.Collections.Generic.IList<T>, System.Collections.IList where T: System.Windows.UIElement
	{
		protected readonly System.Windows.Controls.UIElementCollection ControlCollection;

		protected readonly System.Windows.UIElement StartControl;

		protected readonly System.Windows.UIElement EndControl;

		private int? _StartIndex;
		protected int StartIndex
		{
			get
			{
				if (this._StartIndex == null)
					if (this.StartControl == null)
						this._StartIndex = 0;
					else this._StartIndex = this.ControlCollection.IndexOf(this.StartControl) + 1;
				return this._StartIndex.Value;
			}
		}

		private T[] _Controls;
		protected T[] Controls
		{
			get
			{
				if(this._Controls== null)
				{
					System.Collections.Generic.List<T> controls = new List<T>();
					for(int i=this.StartIndex;i<this.ControlCollection.Count;i++)
					{
						System.Windows.UIElement control = this.ControlCollection[i];
						if (control == this.EndControl)
							break;
						else controls.Add((T)control);
					}
					this._Controls = controls.ToArray();

					//bool start = this.StartControl == null;
					//foreach (System.Windows.UIElement control in this.ControlCollection)
					//{
					//	if (start)
					//	{
					//		if (control == this.EndControl)
					//			break;
					//		else controls.Add(control);
					//	}
					//	else start = control == this.StartControl;
					//}
					//this._Controls = controls.ToArray();
				}
				return this._Controls;
            }
		}

		public double OffsetWidth
		{
			get
			{
				return this.Controls.Sum(c =>
				{
					System.Windows.FrameworkElement control = (System.Windows.FrameworkElement)(System.Windows.UIElement)c;
					return control.Margin.Left + control.Margin.Right + control.ActualWidth;
				});
			}
		}

		public void Refresh()
		{
			this._StartIndex = null;
			this._Controls = null;
		}

		public IgnoreControlList(System.Windows.Controls.UIElementCollection baseList, System.Windows.UIElement endControl)
		{
			this.ControlCollection = baseList;
			this.EndControl = endControl;
		}

		public IgnoreControlList(System.Windows.Controls.UIElementCollection baseList, System.Windows.UIElement startControl, System.Windows.UIElement endControl)
		{
			this.ControlCollection = baseList;

			this.StartControl = startControl;
			this.EndControl = endControl;
		}

		public T this[int index]
		{
			get
			{
				return this.Controls[index];
			}

			set
			{
				this.ControlCollection[this.StartIndex + index] = value;
            }
		}

		public int Count
		{
			get
			{
				return this.Controls.Length;
			}
		}

		public bool IsReadOnly
		{
			get
			{
				return false;
			}
		}

		bool IList.IsReadOnly
		{
			get
			{
				return this.IsReadOnly;
			}
		}

		bool IList.IsFixedSize
		{
			get
			{
				return false;
			}
		}

		int ICollection.Count
		{
			get
			{
				return this.Count;
			}
		}

		object ICollection.SyncRoot
		{
			get
			{
				return this.ControlCollection.SyncRoot;
			}
		}

		bool ICollection.IsSynchronized
		{
			get
			{
				return this.ControlCollection.IsSynchronized;
			}
		}

		object IList.this[int index]
		{
			get
			{
				return this[index];
			}

			set
			{
				this[index] = (T)value;
			}
		}

		public void Add(T item)
		{
			this.ControlCollection.Insert(this.StartIndex + this.Count, item);
		}

		public void Clear()
		{
			this.ControlCollection.RemoveRange(this.StartIndex, this.Count);
		}

		public bool Contains(T item)
		{
			return this.Controls.Contains(item);
		}

		public void CopyTo(T[] array, int arrayIndex)
		{
			this.Controls.CopyTo(array, arrayIndex);
		}

		public IEnumerator<T> GetEnumerator()
		{
			return this.Controls.AsEnumerable().GetEnumerator();
		}

		public int IndexOf(T item)
		{
			return System.Array.IndexOf(this.Controls, item);
		}

		public void Insert(int index, T item)
		{
			this.ControlCollection.Insert(this.StartIndex + index, item);
		}

		public bool Remove(T item)
		{
			int index = this.ControlCollection.IndexOf(item);
			if (index < 0)
				return false;
			this.ControlCollection.RemoveAt(index);
			return true;
		}

		public void RemoveAt(int index)
		{
			if (index >= this.Controls.Length)
				throw new IndexOutOfRangeException("索引超出集合范围！");
			this.ControlCollection.RemoveAt(this.StartIndex + index);
		}

		IEnumerator IEnumerable.GetEnumerator()
		{
			return this.Controls.GetEnumerator();
		}

		int IList.Add(object value)
		{
			this.Add((T)value);
			return this.Count - 1;
		}

		bool IList.Contains(object value)
		{
			return this.Contains((T)value);
		}

		void IList.Clear()
		{
			this.Clear();
		}

		int IList.IndexOf(object value)
		{
			return this.IndexOf((T)value);
		}

		void IList.Insert(int index, object value)
		{
			this.Insert(index, (T)value);
		}

		void IList.Remove(object value)
		{
			this.Remove((T)value);
		}

		void IList.RemoveAt(int index)
		{
			this.RemoveAt(index);
		}

		void ICollection.CopyTo(Array array, int index)
		{
			System.Array.Copy(this.Controls, 0, array, index, this.Controls.Length);
		}
	}

	public class IgnoreControlList : IgnoreControlList<System.Windows.UIElement>
	{

		public IgnoreControlList(System.Windows.Controls.UIElementCollection baseList, System.Windows.UIElement endControl):base(baseList, endControl)
		{
		}

		public IgnoreControlList(System.Windows.Controls.UIElementCollection baseList, System.Windows.UIElement startControl, System.Windows.UIElement endControl):base(baseList,startControl, endControl)
		{
		}
	}
}